package com.oceansoft.mobile.econsole.modules.api.service.impl;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Strings;
import com.oceansoft.mobile.econsole.cache.redis.CacheBaseService;
import com.oceansoft.mobile.econsole.common.constant.ConfigInfo;
import com.oceansoft.mobile.econsole.common.util.HttpClientUtil;
import com.oceansoft.mobile.econsole.modules.api.service.IMediaService;
import com.oceansoft.mobile.econsole.modules.base.entity.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * ClassName: MediaServiceImpl
 * Desc:
 * Date： 2016/12/19
 * Created：shaom
 */
@Service
public class MediaServiceImpl extends CacheBaseService implements IMediaService {

    @Resource
    private ConfigInfo configInfo;

    @Override
    public Result getOrgInfo() throws IOException {
        String key = "api:media:getOrgInfo";
        String res = this.get(key);
        if (Strings.isNullOrEmpty(res)) {
            res = HttpClientUtil.doGet(configInfo.getGadatingServerHost() + "/WebApi/VideoImg/GetOrgInfo");
        }
        this.cache(key, res, 60 * 10);
        Result result = JSON.parseObject(res, Result.class);
        return result;
    }

    @Override
    public Result getVideoImg(String id) throws IOException {
        String key = "api:media:getVideoImg:" + id;
        String res = this.get(key);
        if (Strings.isNullOrEmpty(res)) {
            res = HttpClientUtil.doGet(configInfo.getGadatingServerHost() + "WebApi/VideoImg/GetVideoImg", "sbId", id);
        }
        this.cache(key, res, 60 * 3);
        Result result = JSON.parseObject(res, Result.class);
        return result;
    }
}
